package com.schoolCommunity.util;

import com.schoolCommunity.entity.Admin;
import com.schoolCommunity.mapper.AdminMapper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Set;

@Component
public class CheckInsertAdmin {

    @Resource
    private AdminMapper AdminMapper;

    Logger log = Logger.getLogger(CheckInsertAdmin.class);


    //获取到要注册的member对象
    public String CheckInsertMember(Admin admin) {

        //获取当前年份
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);

        //获取已经注册的邮箱
        Set<String> allMails = AdminMapper.getAllAdminEmailsForSet();

        if (allMails.contains(admin.getEmail()))  //如果存在此邮，直接返回信息
            return "这个邮箱已经被注册";
        if (admin.getId() != null && admin.getId() != 0)   //检测伪造ID
        {
            log.info("获取到的用户id是" + admin.getId());
            return "用户注册不能指定ID，注册ID必须为空";
        }

        if (admin.getRealName() == null) {
            log.info("获取到的管理员姓名是" + admin.getRealName() + "为空或过长");
            return "管理员注册姓名不能为空";
        }

        if (admin.getStudentNumber() == null) {
            return "学号不能为空";
        }

        if (!admin.getEmail().matches("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")) {
            log.info("获取到的用户注册邮箱是" + admin.getEmail());
            return "邮箱格式不正确";
        }

        if (admin.getEmail().length() >= 30) {
            log.info("获取到的用户注册邮箱是" + admin.getEmail() + "过长");
            return "邮箱字符长度不能超过30个字符";
        }

        if (admin.getPassword().length() > 16) {
            log.info("获取到用户注册的密码是" + admin.getPassword() + "过长");
            return "密码长度不能超过16个字符";
        }

        if (admin.getGrade() > year || admin.getGrade() < year - 4) {
            log.info("用户注册的年级不在校" + admin.getGrade());
            return "现阶段这个年级不在校";
        }

        if (admin.getPhone().length() > 12) {
            log.info("注册电话号码非法");
            return "注册电话号码非法";
        }


        return "数据安全";

    }
}
